package com.yz.rabbitmqspringboot;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MainConfig {

    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Bean
    public FanoutExchange springBootExchange(){
        return new FanoutExchange("springboot-fanout-exchange",true,false);
    }

    @Bean
    public Queue fanoutQueue(){
        return new Queue("springboot-fanout-queue",true,false, false);
    }

    @Bean
    public Binding fanoutQueueBingding(){
        return BindingBuilder.bind(fanoutQueue()).to(springBootExchange());
    }

    @Bean(name = "myConsumerlistenerContainer")
    public SimpleRabbitListenerContainerFactory mqConsumerlistenerContainer(){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPrefetchCount(1);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }
}

